import qs from 'qs'
import {removeToken, removeUser, setToken, setUser} from "@/utils/auth";
import {getUserById, login, register} from "@/api/user";

const {getToken, getUser} = require("../../utils/auth");
const getDefaultState = () => {
    return {
        token: getToken(),
        user: getUser()
    }
}
const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_USER: (state, user) => {
        state.user = qs.stringify(user)
    },
}

const actions = {
    // user login
    login({commit}, userInfo) {
        const {username, password} = userInfo
        return new Promise((resolve, reject) => {
            login({username: username.trim(), password: password}).then(response => {
                const {data} = response
                if (data.code !== 20000) {
                    reject(data.message)
                } else {
                    console.log(data.data.user)
                    commit('SET_TOKEN', data.data.token)
                    commit('SET_USER', data.data.user)
                    setToken(data.data.token)
                    setUser(data.data.user)
                    resolve()
                }
            }).catch(error => {
                reject(error)
            })
        })
    },

    register({commit}, userInfo) {
        const {username, password, email} = userInfo
        return new Promise((resolve, reject) => {
            register({username: username.trim(), password: password.trim(), email: email.trim()}).then(response => {
                const {data} = response
                if (data.code !== 20000) {
                    reject(data.message)
                } else {
                    commit('SET_TOKEN', data.data.token)
                    commit('SET_USER', data.data.user)
                    setToken(data.data.token)
                    setUser(data.data.user)
                    resolve()
                }
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({commit, state}) {
        return new Promise((resolve, reject) => {
            // getInfo(state.token).then(response => {
            //     const {data} = response.data
            //     console.log(data)
            //     if (!data) {
            //         return reject('Verification failed, please Login again.')
            //     }
            //
            //     const {name, avatar} = data
            //
            //     commit('SET_NAME', name)
            //     commit('SET_AVATAR', avatar)
            //     resolve(data)
            // }).catch(error => {
            //     reject(error)
            // })
            resolve()
        })
    },

    // user logout
    logout({commit, state}) {
        return new Promise((resolve, reject) => {
            logout(state.token).then(() => {
                removeToken() // must remove  token  first
                removeUser()
                commit('RESET_STATE')
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    getUserInfoById({commit, state}) {
        return new Promise((resolve, reject) => {
            const {id} = qs.parse(state.user)
            getUserById(id).then(response => {
                const {data} = response.data
                if (!data) {
                    return reject('Verification failed, please Login again.')
                }

                // commit('SET_TOKEN', data.data.token)
                commit('SET_USER', data)
                // setToken(data.data.token)
                setUser(data)
                // const {username, avatar} = data
                // const httpAvatar = Vue.prototype.$getImg(avatar)
                // commit('SET_NAME', username)
                // commit('SET_AVATAR', httpAvatar)
                resolve(data)
            }).catch(err => {
                reject(err)
            })
        })
    },

    // remove token
    resetUserToken({commit}) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            removeUser()
            commit('RESET_STATE')
            location.reload()
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
